﻿using DapperExtensions;
using KD.Service.HR.Utility;
using Logistics.Model;
using Logistics.Model.QueryModel;
using Logistics.Service;
using Logistics.WebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace Logistics.WebApi.Controllers
{
    [MyAuthority]
    public class CompanyNoticeController : ApiController
    {
        private CompanyNoticeService service = new CompanyNoticeService();
        private CompanyService companyService = new CompanyService();

        [HttpGet]
        public ResponseModel Get(int id)
        {
            var responseModel = new ResponseModel();
            try
            {
                responseModel.mod = service.Get(id);
                responseModel.InitResultState(GetDataStateEnum.Success);
            }
            catch (Exception ex)
            {
                responseModel.InitResultState(ex);
            }
            return responseModel;
        }

        [HttpGet]
        public ResponseModel DeleteNotice(int id)
        {
            var responseModel = new ResponseModel();
            try
            {
              var noticemod=  service.Get(id);
                wuliu_company_notice mod = new wuliu_company_notice();
                mod.id = noticemod.id;
                mod.idelete = 10;
                responseModel.mod = service.DeleteNotice(mod);
                responseModel.InitResultState(GetDataStateEnum.Success);
            }
            catch (Exception ex)
            {
                responseModel.InitResultState(ex);
            }
            return responseModel;
        }

        [HttpPost]
        public ResponseModel GetPageList(NoticeQuery query)
        {
            if (query == null)
                query = new NoticeQuery();
            var noticeQuery = new List<IPredicate>();
            var resultmod = new ResponseModel();try
            {
                  var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                //var compayid = accountService.GetCompanyId(accountId);
                var company = companyService.GetCompanyByAccountId(accountId);
                if (query.release_time1 > 0)
                    noticeQuery.Add(Predicates.Field<wuliu_company_notice>(f => f.release_time, Operator.Ge, query.release_time1));
                if (query.release_time2 > 0)
                    noticeQuery.Add(Predicates.Field<wuliu_company_notice>(f => f.release_time, Operator.Gt, query.release_time2));
                noticeQuery.Add(Predicates.Field<wuliu_company_notice>(f => f.company_no, Operator.Eq, company.company_no));
                noticeQuery.Add(Predicates.Field<wuliu_company_notice>(f => f.idelete, Operator.Eq, 1));
                var predicate = Predicates.Group(GroupOperator.And, noticeQuery.ToArray());
                IList<ISort> sorts = new List<ISort>() { 
                    Predicates.Sort<wuliu_company_notice>(p=>p.id)
                };
                var noticeList = service.GetPageList(predicate, sorts, query.page - 1, query.pagesize);
                resultmod.mod = noticeList;
                if (noticeList.TotalNum > 0)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                }
                else
                {
                    resultmod.InitResultState(GetDataStateEnum.Error01);
                }
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }
    }
}